package com.steel.yfmall.cloud.assistant.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.bean.ResponseZgMsgBean;
import com.steel.yfmall.cloud.assistant.stock.SteelZgFreightInfoBeanService;
import com.steel.yfmall.cloud.assistant.dto.para.stock.ParaSteelZgFreightInfoBean;
import com.steel.yfmall.cloud.assistant.po.stock.SteelBaseFreightBean;
import com.steel.yfmall.cloud.assistant.po.stock.SteelZgFreightInfoBean;
import com.steel.yfmall.cloud.components.mq.bean.RequestSendMQBean;
import com.steel.yfmall.cloud.components.mq.service.MqSendService;
import com.steel.yfmall.cloud.components.utils.utils.MobileBaseBeanUtils;
import com.steel.yfmall.cloud.components.utils.utils.ResultCodeConstants;
import com.steel.yfmall.cloud.components.utils.utils.UtilDate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class SteelBaseFreightSyncController {

    @Resource
    private SteelZgFreightInfoBeanService steelZgFreightInfoBeanService;

    @Resource
    private MqSendService mqSendService;


    @RequestMapping("doFreightInfo")
    public ResponseZgMsgBean<Boolean> doFreightInfo(@RequestBody List<ParaSteelZgFreightInfoBean> paraBeanList){
        if(log.isDebugEnabled()){
            log.info("function doFreightInfo :" + JSON.toJSONString(paraBeanList));
        }
        ResponseZgMsgBean<Boolean> returnBean = new ResponseZgMsgBean<Boolean>();
        returnBean.setResult(false);
        if(paraBeanList == null || CollectionUtils.isEmpty(paraBeanList)){
            returnBean.setMsg(ResultCodeConstants.INVALID_MSG);
            return returnBean;
        }

        for (ParaSteelZgFreightInfoBean paraBean : paraBeanList){
            if(paraBean == null || StringUtils.isBlank(paraBean.getFreightPriceNo())){
                log.error("同步运费数据，必传参数为空："+JSON.toJSONString(paraBean));
                returnBean.setMsg(ResultCodeConstants.INVALID_MSG);
                return returnBean;
            }
            SteelBaseFreightBean record = new SteelBaseFreightBean();
            //运费单据编号
            record.setName(paraBean.getFreightPriceNo());
            record.setUpdateDate(UtilDate.getDate());
            record.setUpdateUser("SYNC");
            record.setCreateUser("SYNC");
            //起点代码
            if(StringUtils.isNotBlank(paraBean.getBeginLocNo())){
                record.setBeginLocNo(paraBean.getBeginLocNo());
            }
            //起代碼说明 地区信息
            if(StringUtils.isNotBlank(paraBean.getBeginLocNoExp())){
                try{
                    JSONObject regionObject= JSONObject.parseObject(paraBean.getBeginLocNoExp());
                    if(regionObject != null){
                        //区域名称
                        if(regionObject.containsKey("siteChnName") && StringUtils.isNotBlank(regionObject.get("siteChnName").toString())){
                            record.setDepotCode(regionObject.get("siteChnName").toString());
                        }
                    }
                } catch (Exception e){
                    log.error("地区信息转换失败：",e);
                }
            }
            //产品形态
            if(StringUtils.isNotBlank(paraBean.getProdType())){
                record.setProdType(paraBean.getProdType());
            }
            //运输方式代码
            if(StringUtils.isNotBlank(paraBean.getTransWayNo())){
                record.setTransWayNo(paraBean.getTransWayNo());
            }
            //终点代码
            if(StringUtils.isNotBlank(paraBean.getEndLocNo())){
                record.setEndLocNo(paraBean.getEndLocNo());
            }
            //終點代碼说明 地区信息
            if(StringUtils.isNotBlank(paraBean.getEndLocNoExp())){
                try{
                    JSONObject regionObject= JSONObject.parseObject(paraBean.getEndLocNoExp());
                    if(regionObject != null){
                        //地点代码
                        if(regionObject.containsKey("areaNo") && StringUtils.isNotBlank(regionObject.get("areaNo").toString())){
                            record.setAreaNo(regionObject.get("areaNo").toString());
                        }
                        //地区id
                        if(regionObject.containsKey("conty") && StringUtils.isNotBlank(regionObject.get("conty").toString())){
                            record.setRegionId(regionObject.get("conty").toString());
                            record.setRegionType(3);
                        } else if(regionObject.containsKey("city") && StringUtils.isNotBlank(regionObject.get("city").toString())){
                            record.setRegionId(regionObject.get("city").toString());
                            record.setRegionType(2);
                        }
                        //区域名称
                        if(regionObject.containsKey("siteChnName") && StringUtils.isNotBlank(regionObject.get("siteChnName").toString())){
                            record.setRegionName(regionObject.get("siteChnName").toString());
                        }
                    }
                } catch (Exception e){
                    log.error("地区信息转换失败：",e);
                }
            }
            //产品大类
            if(StringUtils.isNotBlank(paraBean.getProdClass())){
                record.setProdClass(paraBean.getProdClass());
            }
            //生效日期
            if(StringUtils.isNotBlank(paraBean.getWorkDay())){
                record.setWorkDate(UtilDate.getDateFromBaseStr(paraBean.getWorkDay()));
            }
            //载重类型 1大载、2标载
            if(StringUtils.isNotBlank(paraBean.getLoadWgtKind())){
                record.setLoadWgtKind(paraBean.getLoadWgtKind());
            }
            //产品长度类型  3:6米；1:9米；2:12米
            if(StringUtils.isNotBlank(paraBean.getLengthKind())){
                record.setLengthKind(paraBean.getLengthKind());
            }
            //是否超长 Y是N否
            if(StringUtils.isNotBlank(paraBean.getOverLength())){
                record.setOverLength(paraBean.getOverLength());
            }
            //是否超宽 Y是N否
            if (StringUtils.isNotBlank(paraBean.getWidthKind())) {
                record.setWidthKind(paraBean.getWidthKind());
            }
            //终点类型 M市场、F工地
            if(StringUtils.isNotBlank(paraBean.getSiteKind())){
                record.setSiteKind(paraBean.getSiteKind());
            }
            //运输工具代码 非尺：Y是；N否
            if(StringUtils.isNotBlank(paraBean.getTransToolNo())){
                record.setTransToolNo(paraBean.getTransToolNo());
            }
            //运费/吨   未税运输单价
            if(paraBean.getPriceMtNoTax() != null){
                record.setShippingFee(paraBean.getPriceMtNoTax());
            }
            //运费/吨（含税元）  含税运输单价
            if(paraBean.getPriceMt() != null){
                record.setShippingFeeMt(paraBean.getPriceMt());
            }
            //状态匹配
            if(StringUtils.isNotBlank(paraBean.getStatus())){
                record.setStatus(StringUtils.endsWithIgnoreCase(paraBean.getStatus(),"Y") ? 1 : 0);
            }
            //创建人
            if(StringUtils.isNotBlank(paraBean.getCreateEmpNo())){
                record.setCreateUser(paraBean.getCreateEmpNo());
            }
            if(StringUtils.isNotBlank(paraBean.getCreateDate()) && StringUtils.isNotBlank(paraBean.getCreateTime())){
                record.setCreateDate(UtilDate.getDateFromStr(paraBean.getCreateDate() + paraBean.getCreateTime()));
            }
            //修改人员
            if(StringUtils.isNotBlank(paraBean.getUpdateEmpNo())){
                record.setUpdateUser(paraBean.getUpdateEmpNo());
            }
            record.setUpdateDate(UtilDate.getDate());

            //新增同步日志 丢取同步mq
            RequestSendMQBean requestSendMQBean = new RequestSendMQBean();
            requestSendMQBean.setSendMqTitle("freight_info_synchronization");
            requestSendMQBean.setSendMqMessage(JSON.toJSONString(record));
            mqSendService.sendCommonMQMessage(requestSendMQBean);
            SteelZgFreightInfoBean insertSteelZgFreightRcord = new SteelZgFreightInfoBean();
            MobileBaseBeanUtils.copyAllFields(paraBean,insertSteelZgFreightRcord);
            if(!steelZgFreightInfoBeanService.insertSteelZgFreightInfo(insertSteelZgFreightRcord)){
                log.error("新增运费同步日志失败"+JSON.toJSONString(insertSteelZgFreightRcord));
            }
        }

        returnBean.setCode(ResultCodeConstants.SUCCESS);
        returnBean.setMsg("已进入执行队列！");
        returnBean.setResult(true);
        return returnBean;
    }
}
